package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.CompanyMapper;
import com.jd.finance.core.mapper.UserMapper;
import com.jd.finance.core.pojo.entity.Company;
import com.jd.finance.core.pojo.entity.User;
import com.jd.finance.core.pojo.dto.CompInfoDTO;
import com.jd.finance.core.service.CompanyService;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.RegexValidateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 公司表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-17
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;


    @Resource
    private UserMapper userMapper;

    /**
     * 根据uid查询企业
     *
     * @param uid 用户id
     * @return
     */
    @Override
    public BaseResult getCompId(Integer uid) {
        QueryWrapper<Company> qw = new QueryWrapper<>();
        qw.lambda().select(Company::getCid).eq(Company::getUid, uid);
        List<Company> companyList = companyMapper.selectList(qw);
        return BaseResult.ok().data("list", companyList);
    }

    /**
     * 用户进行企业认证
     *
     * @param uid           用户id
     * @param compUser      法人姓名
     * @param compAddress   企业地址
     * @param compNumber    证件编号
     * @param compName      企业名称
     * @param businessScope 经营范围
     * @param compPhone     企业电话
     * @return
     */
    @Override
    public BaseResult compCertifica(Integer uid, String compUser, String compAddress, String compNumber, String compName, String businessScope, String compPhone) {
        if (!StringUtils.isAllBlank(String.valueOf(uid), compUser, compAddress, compNumber, compName, businessScope, compPhone)) {
            log.error("认证信息为空！");
            return BaseResult.error().message("认证信息为空!");
        }

        //判断用户已注册
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        List<User> users = userMapper.selectList(queryWrapper);
        if (users == null || users.size() == 0) {
            log.error("用户未注册！");
            return BaseResult.error().message("用户未注册！");
        }

        //判断证件编号格式是否正确
        boolean validCertifica = RegexValidateUtils.checkCertifica(compNumber);
        if (!validCertifica) {
            log.error("证件编号格式不正确！");
            return BaseResult.error().message("证件编号格式不正确！");
        }

        //判断联系电话格式是否正确
        boolean validPhone = RegexValidateUtils.checkCellphone(compPhone);
        if (!validPhone) {
            log.error("联系电话格式不正确");
            return BaseResult.error().message("联系电话格式不正确！");
        }

        //判断  法人姓名 和  注册用户姓名 是否一致
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("uid",uid);
        List<User> list=userMapper.selectList(userQueryWrapper);
        if(list==null && list.size()==0) {
            log.error("法人姓名与登录用户不一致！");
            return BaseResult.error().message("法人姓名与登录用户不一致！");
        }

        //添加企业信息
        Company company = new Company();
        company.setCompAddress(compAddress);
        company.setCompNumber(compNumber);
        company.setCompName(compName);
        company.setCompPhone(compPhone);
        company.setBusinessScope(businessScope);
        company.setUid(uid);

        int count = companyMapper.insert(company);
        if (count == 0) {
            log.error("认证失败!");
            return BaseResult.error().message("认证失败！");
        }
        return BaseResult.ok().data("renzhengjieguo", 1);
    }

    /**
     * 查询企业信息
     *
     * @param uid
     * @return
     */
    @Override
    public BaseResult getCompInfo(Integer uid) {
        List<CompInfoDTO> list = companyMapper.selectCompInfo(uid);
        if (CollectionUtils.isEmpty(list)) {
            log.error("查询企业信息失败");
            return BaseResult.error().message("查询企业信息失败！");
        }
        return BaseResult.ok().data("compList", list).message("查询企业信息成功!");
    }

    /**
     * 查询是否进行企业认证
     *
     * @param uid
     * @return
     */
    @Override
    public BaseResult getCompCertitica(Integer uid) {
        QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
        companyQueryWrapper.eq("uid", uid);
        int count = companyMapper.selectCount(companyQueryWrapper);
        if (count == 0) {
            log.error("用户未进行企业认证!");
            return BaseResult.error().message("用户未进行企业认证！");
        }
        return BaseResult.ok().data("cxjieguo", 1);
    }
}
